<?php
/**
 * @title 449. 序列化和反序列化二叉搜索树 - 层次遍历
 * @author start2004
 */

class Codec {
    function __construct() {

    }

    /**
     * @param TreeNode $root
     * @return String
     */
    function serialize($root) {
        /**
         * @since 2021-08-09 返回数组
         */
        $result = [];

        /**
         * @since 2021-08-09 层次遍历
         */
        $nodeArray = [$root];
        while($nodeArray){
            $nextArray = [];

            /**
             * @since 2021-08-09 遍历点
             */
            foreach ($nodeArray as $node){
                if($node === null){
                    $result[] = null;
                } else {
                    $result[] = $node->val;

                    /**
                     * @since 2021-08-09 左右子树
                     */
                    $nextArray[] = $node->left;
                    $nextArray[] = $node->right;
                }
            }

            /**
             * @since 2021-08-09 下次迭代
             */
            $nodeArray = $nextArray;
        }

        /**
         * @since 2021-08-09 收官, 去掉尾部null
         */
        for($i=count($result)-1; $i>=0; $i--){
            if($result[$i] === null){
                unset($result[$i]);
            } else {
                break;
            }
        }

        /**
         * @since 2021-08-09 序列化返回
         * @return
         */
        return json_encode($result, JSON_UNESCAPED_UNICODE);
    }

    /**
     * @param String $data
     * @return TreeNode
     */
    function deserialize($data) {
        /**
         * @since 2021-08-09 反序列化数组
         */
        $dataArray = json_decode($data, true);

        /**
         * @since 2021-08-09 长度
         */
        $len = count($dataArray);

        /**
         * @since 2021-08-09 返回树
         */
        $tree = new TreeNode($dataArray[0]);

        /**
         * @since 2021-08-09 跳出
         */
        $break = false;

        /**
         * @since 2021-08-09 层次遍历
         */
        $i = 1;
        $nodeArray = [$tree];
        while($nodeArray){
            $nextArray = [];

            /**
             * @since 2021-08-09 避免每次都要判断 i<len
             */
            $need = $i + count($nodeArray)*2;
            if($need > $len){
                $break = true;

                /**
                 * @since 2021-08-09 dataArray补数据
                 */
                $dataArray = array_merge($dataArray, array_fill(0, $need-$len, null));
            } else {}


            /**
             * @since 2021-08-09 遍历节点
             */
            foreach ($nodeArray as $node){
                /**
                 * @since 2021-08-09 左子树
                 */
                $val = $dataArray[$i];
                if($val !== null){
                    $node->left = new TreeNode($val);
                    $nextArray[] = $node->left;
                } else {}
                $i++;

                /**
                 * @since 2021-08-09 右子树
                 */
                $val = $dataArray[$i];
                if($val !== null){
                    $node->right = new TreeNode($val);
                    $nextArray[] = $node->right;
                } else {}
                $i++;
            }

            /**
             * @since 2021-08-09 跳出
             */
            if($break){
                break;
            } else {}

            /**
             * @since 2021-08-09 下次迭代
             */
            $nodeArray = $nextArray;
        }

        /**
         * @return
         */
        return $tree;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.28_11.php
 */
$datas = [
    [1,2,3,null,4],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new CodeC();
    $result = $obj->deserialize(json_encode($data));
    var_dump($result);
    $result = $obj->serialize($result);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
